<template>
  <div class="quick-actions-component">
    <el-card class="actions-card" :class="{ 'edit-mode': isEditMode }">
      <!-- 编辑模式工具栏 -->
      <div v-if="isEditMode" class="component-toolbar">
        <el-button-group size="small">
          <el-button @click="$emit('configure')" icon="Setting" circle />
          <el-button @click="$emit('toggle-visibility')" :icon="component.visible ? 'View' : 'Hide'" circle />
          <el-button @click="$emit('remove')" icon="Delete" type="danger" circle />
        </el-button-group>
      </div>

      <!-- 组件标题 -->
      <template #header>
        <div class="card-header">
          <span class="card-title">{{ component.title }}</span>
          <el-button
            v-if="isEditMode"
            type="primary"
            size="small"
            @click="$emit('configure-component', componentConfig)"
          >
            <el-icon><Setting /></el-icon>
            配置
          </el-button>
          <el-button @click="customizeActions" size="small" icon="Setting">
            自定义
          </el-button>
        </div>
      </template>

      <!-- 快捷操作内容 -->
      <div class="actions-content">
        <div class="actions-grid" :class="`layout-${config.layout || 'grid'}`">
          <div 
            v-for="action in visibleActions" 
            :key="action.key"
            class="action-item"
            :class="action.type"
            @click="handleAction(action)"
          >
            <div class="action-icon" :style="{ backgroundColor: action.color }">
              <el-icon>
                <component :is="action.icon" />
              </el-icon>
            </div>
            <div class="action-content">
              <div class="action-title">{{ action.title }}</div>
              <div class="action-description">{{ action.description }}</div>
              <div class="action-badge" v-if="action.badge">
                <el-badge :value="action.badge" :type="action.badgeType || 'primary'" />
              </div>
            </div>
            <div class="action-arrow" v-if="config.layout !== 'compact'">
              <el-icon><ArrowRight /></el-icon>
            </div>
          </div>
        </div>
        
        <!-- 更多操作 -->
        <div class="more-actions" v-if="hiddenActions.length > 0">
          <el-button @click="showMoreActions" type="text" size="small">
            查看更多 ({{ hiddenActions.length }})
            <el-icon><ArrowDown /></el-icon>
          </el-button>
        </div>
      </div>
    </el-card>
    
    <!-- 更多操作抽屉 -->
    <el-drawer
      v-model="moreActionsVisible"
      title="更多快捷操作"
      direction="rtl"
      size="400px"
    >
      <div class="drawer-actions">
        <div 
          v-for="action in hiddenActions" 
          :key="action.key"
          class="drawer-action-item"
          @click="handleAction(action)"
        >
          <div class="action-icon" :style="{ backgroundColor: action.color }">
            <el-icon>
              <component :is="action.icon" />
            </el-icon>
          </div>
          <div class="action-content">
            <div class="action-title">{{ action.title }}</div>
            <div class="action-description">{{ action.description }}</div>
          </div>
          <div class="action-badge" v-if="action.badge">
            <el-badge :value="action.badge" :type="action.badgeType || 'primary'" />
          </div>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { 
  Plus, 
  ShoppingCart, 
  Wallet, 
  UserFilled,
  Box,
  TrendCharts,
  Document,
  Setting,
  ArrowRight,
  ArrowDown,
  Bell,
  Search,
  Upload,
  Download
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  component: {
    type: Object,
    required: true
  },
  componentConfig: {
    type: Object,
    default: () => ({})
  },
  isEditMode: {
    type: Boolean,
    default: false
  },
  visible: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['configure', 'toggle-visibility', 'remove', 'configure-component'])

const router = useRouter()
const moreActionsVisible = ref(false)
const maxVisibleActions = ref(6)

const config = computed(() => props.component.config || {})

// 所有快捷操作
const allActions = ref([
  {
    key: 'new-order',
    title: '新建订单',
    description: '创建新的销售订单',
    icon: Plus,
    color: '#409eff',
    type: 'primary',
    route: '/sales/orders/create',
    badge: null,
    visible: true
  },
  {
    key: 'inventory',
    title: '库存管理',
    description: '查看和管理库存',
    icon: Box,
    color: '#67c23a',
    type: 'success',
    route: '/warehouse/inventory',
    badge: 23,
    badgeType: 'warning',
    visible: true
  },
  {
    key: 'customer',
    title: '客户管理',
    description: '管理客户信息',
    icon: UserFilled,
    color: '#e6a23c',
    type: 'warning',
    route: '/basic/customers',
    badge: null,
    visible: true
  },
  {
    key: 'finance',
    title: '财务报表',
    description: '查看财务数据',
    icon: Wallet,
    color: '#f56c6c',
    type: 'danger',
    route: '/finance/reports',
    badge: null,
    visible: true
  },
  {
    key: 'purchase',
    title: '采购管理',
    description: '管理采购订单',
    icon: ShoppingCart,
    color: '#909399',
    type: 'info',
    route: '/purchase/orders',
    badge: 5,
    badgeType: 'danger',
    visible: true
  },
  {
    key: 'reports',
    title: '数据报表',
    description: '查看业务报表',
    icon: TrendCharts,
    color: '#722ed1',
    type: 'primary',
    route: '/reports',
    badge: null,
    visible: true
  },
  {
    key: 'documents',
    title: '文档管理',
    description: '管理业务文档',
    icon: Document,
    color: '#13c2c2',
    type: 'info',
    route: '/documents',
    badge: null,
    visible: false
  },
  {
    key: 'notifications',
    title: '消息通知',
    description: '查看系统通知',
    icon: Bell,
    color: '#fa8c16',
    type: 'warning',
    action: 'showNotifications',
    badge: 12,
    badgeType: 'danger',
    visible: false
  },
  {
    key: 'search',
    title: '全局搜索',
    description: '搜索业务数据',
    icon: Search,
    color: '#52c41a',
    type: 'success',
    action: 'openSearch',
    badge: null,
    visible: false
  },
  {
    key: 'import',
    title: '数据导入',
    description: '导入业务数据',
    icon: Upload,
    color: '#1890ff',
    type: 'primary',
    action: 'importData',
    badge: null,
    visible: false
  },
  {
    key: 'export',
    title: '数据导出',
    description: '导出业务数据',
    icon: Download,
    color: '#eb2f96',
    type: 'danger',
    action: 'exportData',
    badge: null,
    visible: false
  },
  {
    key: 'settings',
    title: '系统设置',
    description: '配置系统参数',
    icon: Setting,
    color: '#595959',
    type: 'info',
    route: '/system/settings',
    badge: null,
    visible: false
  }
])

// 可见的操作
const visibleActions = computed(() => {
  const visible = allActions.value.filter(action => action.visible)
  return visible.slice(0, maxVisibleActions.value)
})

// 隐藏的操作
const hiddenActions = computed(() => {
  const visible = allActions.value.filter(action => action.visible)
  return visible.slice(maxVisibleActions.value)
})

// 处理操作点击
const handleAction = (action) => {
  if (action.route) {
    router.push(action.route)
  } else if (action.action) {
    handleCustomAction(action.action)
  }
  
  // 关闭抽屉
  moreActionsVisible.value = false
}

// 处理自定义操作
const handleCustomAction = (actionType) => {
  switch (actionType) {
    case 'showNotifications':
      ElMessage.info('通知功能开发中...')
      break
    case 'openSearch':
      ElMessage.info('搜索功能开发中...')
      break
    case 'importData':
      ElMessage.info('数据导入功能开发中...')
      break
    case 'exportData':
      ElMessage.info('数据导出功能开发中...')
      break
    default:
      ElMessage.info('功能开发中...')
  }
}

// 显示更多操作
const showMoreActions = () => {
  moreActionsVisible.value = true
}

// 自定义操作
const customizeActions = async () => {
  try {
    await ElMessageBox.confirm(
      '是否要自定义快捷操作？您可以选择显示哪些操作以及它们的顺序。',
      '自定义快捷操作',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }
    )
    
    ElMessage.info('自定义功能开发中...')
  } catch {
    // 用户取消
  }
}
</script>

<style scoped>
.quick-actions-component {
  position: relative;
}

.actions-card {
  border-radius: 8px;
  transition: all 0.3s ease;
}

.actions-card.edit-mode {
  border: 2px dashed #409eff;
  position: relative;
}

.actions-card.edit-mode:hover {
  border-color: #67c23a;
  box-shadow: 0 4px 12px rgba(103, 194, 58, 0.3);
}

.component-toolbar {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 10;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 6px;
  padding: 4px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.actions-content {
  padding: 0;
}

.actions-grid {
  display: grid;
  gap: 12px;
}

.actions-grid.layout-grid {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

.actions-grid.layout-vertical {
  grid-template-columns: 1fr;
}

.actions-grid.layout-compact {
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
}

.action-item {
  display: flex;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid transparent;
  position: relative;
}

.action-item:hover {
  background: #e9ecef;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.action-item.primary {
  border-left-color: #409eff;
}

.action-item.success {
  border-left-color: #67c23a;
}

.action-item.warning {
  border-left-color: #e6a23c;
}

.action-item.danger {
  border-left-color: #f56c6c;
}

.action-item.info {
  border-left-color: #909399;
}

.action-icon {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 18px;
  margin-right: 12px;
  flex-shrink: 0;
}

.action-content {
  flex: 1;
  min-width: 0;
}

.action-title {
  font-size: 14px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.action-description {
  font-size: 12px;
  color: #909399;
  line-height: 1.4;
}

.action-badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

.action-arrow {
  color: #c0c4cc;
  font-size: 14px;
  margin-left: 8px;
}

.layout-compact .action-item {
  flex-direction: column;
  text-align: center;
  padding: 12px;
}

.layout-compact .action-icon {
  margin-right: 0;
  margin-bottom: 8px;
}

.layout-compact .action-arrow {
  display: none;
}

.more-actions {
  text-align: center;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid #e4e7ed;
}

.drawer-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.drawer-action-item {
  display: flex;
  align-items: center;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.drawer-action-item:hover {
  background: #e9ecef;
}

.drawer-action-item .action-icon {
  width: 32px;
  height: 32px;
  font-size: 16px;
  margin-right: 12px;
}

@media (max-width: 768px) {
  .actions-grid.layout-grid {
    grid-template-columns: 1fr;
  }
  
  .actions-grid.layout-compact {
    grid-template-columns: repeat(3, 1fr);
  }
  
  .action-item {
    padding: 12px;
  }
  
  .action-icon {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }
}
</style>